import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {STColumn} from '@delon/abc';
import {SFSchema} from '@delon/form';
import {BaseService} from '@sb/base';
import {SysBackupTemplateService} from '../service/sys-backup-template.service';
import {SysBackupTemplate} from '../entity/sys-backup-template';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
    selector: 'sys-backup-template',
    templateUrl: './sys-backup-template.component.html',
    styleUrls: ['./sys-backup-template.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: SysBackupTemplateService
        }
    ]
})
export class SysBackupTemplateComponent implements OnInit {


    @ViewChild('textAreaElement', {static: false, read: ElementRef})
    textAreaElement: ElementRef;

    selectTemplate: SysBackupTemplate;

    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'templateId'
        }, {
            title: '模板名称',
            index: 'templateName',
        }
    ];

    schema: SFSchema = {
        properties: {
            templateName: {
                type: 'string',
                title: '模板名称',
                minLength: 2
            }
        },
        ui: {
            spanLabelFixed: 100
        },
        required: ['templateName']
    };

    constructor(private sysBackupTemplateService: SysBackupTemplateService,
                private nzMessageService: NzMessageService) {
    }

    ngOnInit() {
    }

    beforeDataRender(v) {
        this.selectTemplate = null;
        return v;
    }

    itemClick(template: SysBackupTemplate) {
        this.selectTemplate = template;

        if (template && template.templateId) {
            this.sysBackupTemplateService.getTemplate(template.templateId).subscribe(returnForm => {
                this.textAreaElement.nativeElement.value = returnForm.message || '';
            });
        }
    }

    dataReload() {
    }

    saveCommand() {
        this.sysBackupTemplateService.saveTemplate(
            this.selectTemplate.templateId,
            this.textAreaElement.nativeElement.value).subscribe(() => {
            this.nzMessageService.info('保存成功！');
        });

    }
}
